package com.baizhi.controller;

import com.baizhi.entity.Figures;
import com.baizhi.entity.ProductSize;
import com.baizhi.entity.Result;
import com.baizhi.entity.Sku;
import com.baizhi.service.ProductSizeService;
import com.baizhi.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("sku")
public class SkuController {

    @Autowired
    private SkuService skuService;

    @Autowired
    private ProductSizeService productSizeService;

    @RequestMapping("findAll")
    @ResponseBody
    public List<Sku> findAll(){
        return skuService.findAll();
    }

    @RequestMapping("save")
    @ResponseBody
    public Result save(Sku sku, ProductSize productSize,String id){
        Result result = null;
        try{
            productSizeService.save(productSize);
            sku.setProductid(id);
            sku.setSizeid(productSize.getId());
            skuService.save(sku);
            result = new Result();
            result.setStatus(true);
        }catch (Exception e){
            e.printStackTrace();
            result =new Result();
            result.setMessage("添加失败");
        }
        return result;
    }


    @RequestMapping("findByProductId")
    @ResponseBody
    public List<Figures> findByProductId(String id){
        List<Figures> lists = new ArrayList<>();
        Figures figures = new Figures();
        List<Sku> byProductId = skuService.findByProductId(id);
        for (Sku sku : byProductId) {
            figures.setSkuId(sku.getId());
            figures.setColor(sku.getColor());
            figures.setNumber(sku.getNumber());
            figures.setProductid(sku.getProductid());
            ProductSize productSize = productSizeService.findById(sku.getSizeid());
            figures.setSizeid(productSize.getId());
            figures.setSize(productSize.getSize());
            figures.setBust(productSize.getBust());
            figures.setCoatlength(productSize.getCoatlength());
            figures.setHipline(productSize.getHipline());
            figures.setWaist(productSize.getWaist());
            lists.add(figures);
        }
        System.out.println(figures);
        return lists;
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(String id){
        Result result = null;
        try{
            skuService.delete(id);
            result = new Result();
            result.setMessage("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            result = new Result();
            result.setMessage("删除失败");
        }
        return result;
    }

    @ResponseBody
    @RequestMapping("update")
    public Result update(Sku sku,ProductSize productSize){
        productSizeService.update(productSize);
        skuService.update(sku);
        Result result = new Result();
        result.setMessage("更新成功");
        return result;
    }
}
